package com.demo2do.fms.admin.dao.business;

import java.util.Date;
import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Execute;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.demo2do.fms.admin.dto.LoadComponent;
import com.demo2do.fms.entity.business.Shipment;

/**
 * 
 * @author Ark
 *
 */
@Dao("hibernate")
public interface ShipmentDao {
    
    /**
     * @param loadNumbers
     * @return
     */
    @Query("SELECT DISTINCT new com.demo2do.fms.admin.dto.LoadComponent(loadNumber, loadId) FROM Shipment WHERE disabled = false AND loadNumber IN (:loadNumbers)")
    public List<LoadComponent> listByLoadNumbers(@Variable("loadNumbers") List<String> loadNumbers);
    
    /**
     * @param itemIds
     * @return
     */
    @Query("FROM Shipment WHERE disabled = false AND id IN (:itemIds)")
    public List<Shipment> listByItemIds(@Variable("itemIds") List<Long> itemIds);
    
    /**
     * @param firstShipmentId
     * @return
     */
    @Query("FROM Shipment WHERE disabled = false AND firstShipmentId = :firstShipmentId")
    public List<Shipment> listByFirstShipmentId(@Variable("firstShipmentId") Long firstShipmentId);
    
    /**
     * 
     * @param dispatchOrderId
     * @return
     */
    @Query("FROM Shipment WHERE disabled = false AND dispatchOrderId = :dispatchOrderId")
    public List<Shipment> listByDispatchOrderId(@Variable("dispatchOrderId") Long dispatchOrderId);
    
    /**
     * 非空车调度
     * 
     * @param dispatchOrderId
     * @return
     */
    @Query("FROM Shipment WHERE disabled = false AND dispatchOrderId = :dispatchOrderId AND loadQuantity != 0")
    public List<Shipment> listBaseByDispatchOrderId(@Variable("dispatchOrderId") Long dispatchOrderId);
    
    /**
     * @param dispatchOrderIds
     * @return
     */
    @Query("FROM Shipment WHERE disabled = false AND dispatchOrderId IN (:dispatchOrderIds)")
    public List<Shipment> listByDispatchOrderIds(@Variable("dispatchOrderIds") List<Long> dispatchOrderIds);
    
    /**
     * load shipment by shipment_id
     * 
     * @param shipmentId
     * @return
     */
    @Query("FROM Shipment WHERE disabled = false AND shipmentId = :shipmentId")
    public Shipment loadByShipmentId(@Variable("shipmentId")Long shipmentId);
    
    /**
     * load shipment by load_number
     * 
     * @param loadNumber
     * @return
     */
    @Query("FROM Shipment WHERE disabled = false AND loadNumber = :loadNumber")
    public Shipment loadByLoadNumber(@Variable("loadNumber")String loadNumber);
    
    /**
     * load shipment by shipment_id and load_number
     * 
     * @param shipmentId
     * @param loadNumber
     * @return
     */
    @Query("FROM Shipment WHERE shipmentId = :shipmentId AND loadNumber = :loadNumber")
    public Shipment load(@Variable("shipmentId") Long shipmentId, @Variable("loadNumber")String loadNumber);
    
    /**
     * load shipment by loadNumber and transCorpIds
     * 
     * @param loadNumber
     * @param transCorpIds
     * @return
     */
    @Query("FROM Shipment WHERE loadNumber = :loadNumber AND transCorpId IN (:transCorpIds) AND disabled = false")
    public Shipment load(@Variable("loadNumber")String loadNumber, @Variable("transCorpIds") List<Long> transCorpIds);
    
    /**
     * list shipment_id by finance_id
     * 
     * @param financeId
     * @return
     */
    @Query("SELECT s.id FROM Shipment s, Finance f WHERE ((s.shipmentId = f.shipmentId AND s.loadNumber = f.loadNumber) OR (f.customerId IS NOT NULL AND s.orderId = f.orderId AND f.firstShipmentId = s.firstShipmentId)) AND s.disabled = false AND f.id = :financeId")
    public List<Long> listShipmentIdByFinanceId(@Variable("financeId") Long financeId);
    
    /**
     * get shipment_id by finance_id
     * 
     * @param financeId
     * @return
     */
    @Query("SELECT s.id FROM Shipment s, Finance f WHERE ((s.shipmentId = f.shipmentId AND s.loadNumber = f.loadNumber) OR (f.customerId IS NOT NULL AND s.orderId = f.orderId AND f.firstShipmentId = s.firstShipmentId)) AND s.disabled = false AND f.id = :financeId")
    public Long getShipmentIdByFinanceId(@Variable("financeId") Long financeId);
    
    /**
     * 更新调度指令回单状态
     * 
     * @param itemIds
     * @param accConfirmDate
     * @param operator
     * @param lastModifiedDate
     */
    @Execute("UPDATE Shipment SET accomplishConfirm = true, accConfirmDate = :accConfirmDate, operator = :operator, lastModifiedDate = :lastModifiedDate WHERE disabled = false AND id IN (:itemIds) AND accomplishConfirm = false")
    public void accomplishConfirm(@Variable("itemIds") List<Long> itemIds, @Variable("accConfirmDate") Date accConfirmDate, @Variable("operator") String operator, @Variable("lastModifiedDate") Date lastModifiedDate);

    /**
     * 运输完成
     * 
     * @param dispatchOrderId
     * @param operator
     * @param lastModifiedDate
     */
    @Execute("UPDATE Shipment SET status = 3, operator = :operator, lastModifiedDate = :lastModifiedDate WHERE disabled = false AND dispatchOrderId = :dispatchOrderId")
    public void finish(@Variable("dispatchOrderId") Long dispatchOrderId, @Variable("operator") String operator, @Variable("lastModifiedDate") Date lastModifiedDate);

}
